import 'package:flutter/material.dart';

void main() => runApp(MyHomePage());

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _count = 0;

  void _incrementCounter() {
    setState(() {
      _count++;
    });
  }

  // state生命周期作用：初始化数据、在合适的生命周期网络请求、事件监听、销毁资源回收内存等
  // 初始化,冷启动调用一次，即组件首次显示时，用于初始化操作如，设置监听事件等
  @override
  void initState() {
    super.initState();
    print('initState');
  }

  // 重新构建组件时调用，父Widget结构发生改变(重新构建了子Widget)时调用
  @override
  void didUpdateWidget(MyHomePage oldWidget) {
    super.didUpdateWidget(oldWidget);
    print('didUpdateWidget');
  }

  // 组件状态变为非活动时
  @override
  void deactivate() {
    super.deactivate();
    print('deactivate');
  }

  // 组件被销毁
  @override
  void dispose() {
    super.dispose();
    print('dispose');
  }

  // 热重载时调用，仅在调试阶段
  @override
  void reassemble() {
    super.reassemble();
    print('reassemble');
  }

  // 依赖发生改变时调用，如更改App风格样式、更改文本等
  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    print('didChangeDependencies');
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '计数器Demo',
      theme: ThemeData(primarySwatch: Colors.blue),
      home: Scaffold(
        appBar: AppBar(
          title: Text('计数器'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text('点击加号增加数值'),
              Text(
                '$_count',
                style: Theme.of(context).textTheme.headline1,
              )
            ],
          ),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: _incrementCounter,
          child: Icon(Icons.add),
          tooltip: '点击添加',
        ),
      ),
    );
  }
}
